﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sispe.Entities;

namespace Sispe.Repository
{
    public class EstadoCivilRepository:MasterRepository, IEstadoCivilRepository
    {
        public List<ESTADO_CIVIL> GetAllFromEstadoCivil()
        {
            var query = from p in context.ESTADO_CIVIL
                        select p;

            return query.ToList();
        }

        public List<ESTADO_CIVIL> GetAllfromEstadoCivilByDescripcion(string criterio)
        {
            if (criterio != string.Empty)
            {
                var query = from p in context.ESTADO_CIVIL
                            where p.Descripcion.ToUpper().Contains(criterio.ToUpper())
                            select p;
                return query.ToList();
            }
            else
            {
                var query = from p in context.ESTADO_CIVIL
                            select p;

                return query.ToList();
            }
        }

        public ESTADO_CIVIL GetFromEstadoCivilById(int id)
        {
            var query = from p in context.ESTADO_CIVIL
                        where p.IdEstadoCivil.Equals(id)
                        select p;

            return query.SingleOrDefault();
        }

        public void AddEstadoCivil(ESTADO_CIVIL estadoCivil)
        {
            estadoCivil.PERSONAs = null;
            context.ESTADO_CIVIL.Add(estadoCivil);
            try
            {
                context.SaveChanges();
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException e)
            {

                var outputLines = new List<string>();
                foreach (var eve in e.EntityValidationErrors)
                {
                    outputLines.Add(string.Format(
                        "{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:",
                        DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State));
                    foreach (var ve in eve.ValidationErrors)
                    {
                        outputLines.Add(string.Format(
                            "- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage));
                    }
                }
                if (System.IO.File.Exists(@"D:\errors.txt"))
                {
                    System.IO.File.Delete(@"D:\errors.txt");
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                else
                {
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                throw;
            }
        }

        public void UpdateEstadoCivil(ESTADO_CIVIL estadoCivil)
        {
            estadoCivil.PERSONAs = null;
            context.Entry(estadoCivil).State = System.Data.EntityState.Modified;
            try
            {
                context.SaveChanges();
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException e)
            {

                var outputLines = new List<string>();
                foreach (var eve in e.EntityValidationErrors)
                {
                    outputLines.Add(string.Format(
                        "{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:",
                        DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State));
                    foreach (var ve in eve.ValidationErrors)
                    {
                        outputLines.Add(string.Format(
                            "- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage));
                    }
                }
                if (System.IO.File.Exists(@"D:\errors.txt"))
                {
                    System.IO.File.Delete(@"D:\errors.txt");
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                else
                {
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                throw;
            }
        }

        public void DeleteEstadoCivil(int id)
        {
            var existe = context.ESTADO_CIVIL.Find(id);

            if (existe != null)
            {
                context.ESTADO_CIVIL.Remove(existe);
                context.SaveChanges();
            }
        }

        public bool ExisteDUplicidad(string criterio, int id)
        {
            var query = from p in context.ESTADO_CIVIL
                        where p.Descripcion.ToUpper().CompareTo(criterio.ToUpper()) == 0 && p.IdEstadoCivil != id
                        select p;

            return query.Any();
        }
    }
}
